<?php

/* $Id: HTMLTemplate.class.php 118 2008-05-01 00:43:21Z joshuarudd $ */

class HTMLTemplate
{
	
	public $js;
	public $css;
	public $cms;
	public $db;
	public $page;
	
	function __construct($cms)
	{
		$this->js = Array();
		$this->css = Array();
		
		$this->cms = $cms;
		$this->db = $cms->db;
		
		$this->loadTemplate();
	}
	
	function htmlTitle()
	{
		$r = '[' . BLACKBIRD_CLIENT . ']';
		return $r;
	}
	
	function loadTemplate()
	{
		include 'templates/Default.template.php';
	}
	
	function content()
	{
		//output buffering
		ob_start();
		$this->cms->pageClass->buildPage();
		$r = ob_get_contents();
		ob_end_clean();
		return $r;
		
		/*
		
		<ul id="bb_main_sections">
			<li><a href="#" id="bb_toggle_main">Main</a></li>
			<li><a href="#" id="bb_toggle_media">Media</a></li>
			<li><a href="#" id="bb_toggle_sponsors">Sponsors</a></li>
			<li><a href="#" id="bb_toggle_comments">Comments</a></li>
		</ul>
		
		<div id="bb_module">
			<div class="section" id="bb_section_main">
				Main
			</div>
			<div class="section" id="bb_section_media">
				Media
			</div>
			<div class="section" id="bb_section_sponsors">
				Sponsors
			</div>
			<div class="section" id="bb_section_comments">
				Comments
			</div>
		</div>';
		*/
		return $r;
	}
	
	function js()
	{
		// Default JS files
		$js[] = 'prototype.js';
		$js[] = 'scriptaculous/scriptaculous.js?load=effects,dragdrop';
		$js[] = 'blackbird.js';
		$js[] = 'datagrid.js';
		$js[] = 'eventbroadcaster.js';
		$js[] = 'functions.js';
		$js[] = 'form.js';
		$js[] = 'validator.js';
		$js[] = 'imagebrowser.js';
		$js[] = 'listmanager.js';
		$js[] = 'cms.js';
		array_merge($js,$this->js);
		// Remove duplicates
		$files = array_unique($js);
		// Build links
		$r = '';
		foreach ($files as $filename) {
			$r .= sprintf(
				'<script src="%s" type="text/javascript"></script>',
				BLACKBIRD_ROOT . ASSETS . 'js/' . $filename
			);
		}
		$r .= '
		<script type="text/javascript">
			<!-- <![CDATA[
			CMS.setProperty("cms_root","' . BLACKBIRD_ROOT . '");
			// ]]> -->
		</script>';
		return $r;
	}
	
	public function breadcrumb()
	{
		$r = '<ol id="bb_breadcrumb">';
		$r .= '<li><a href="'.BLACKBIRD_ROOT.'dashboard">Dashboard</a></li>';
		//switch based upon editing mode
		if($this->cms->table != ''){
			
			$item = $this->cms->table;
			$q_label = $this->db->queryRow("SELECT display_name,menu_id FROM ".BLACKBIRD_TABLE_PREFIX."tables WHERE table_name = '$item'");
			if($q_label['display_name'] == ''){
				$label = Utils::formatHumanReadable($item);
			}else{
				$label = $q_label['display_name'];
			}
			
			$r .= '<li><a href="'.BLACKBIRD_ROOT.'browse/'.$this->cms->table.'">'.$label.'</a></li>';
		}
		if($this->cms->id != ''){
			
			$q_table = $this->db->queryRow("SELECT * FROM ".BLACKBIRD_TABLE_PREFIX."tables WHERE table_name = '$item'");
			if($q_table['display_key'] != ''){
				$r .= '<li>Edit Record: '.$this->cms->pageClass->row_data[$q_table['display_key']] . '</li>';
			}else{
				$r .= '<li>Edit Record</li>';
			}
			//$r .= '<li>Edit Record: (key from javascript)</li>';
		}
					
		$r .= '</ol>';
		return $r;
	}
	
	public function session()
	{
		$r = '<ul>';
		if($this->cms->session->logged===true){
			$r .= '<li>Logged in as <a href="'.BLACKBIRD_ROOT.'user">'.$this->cms->session->displayname.'</a></li>';
			$r .= '<li><a href="'.BLACKBIRD_ROOT.'admin">Admin</a></li>';
			$r .= '<li><a href="'.BLACKBIRD_ROOT.'logout">Logout</a></li>';
		}
		$r .= '<li>Blackbird '.BLACKBIRD_VERSION.'</li></ul>';
		return $r;
	}
	
	public function logo()
	{
		return '<div id="bb_logo"><a href="'.BLACKBIRD_ROOT.'dashboard"><img src="#" alt="COMPANY LOGO" /></a></div>';
	}
	
	public function tables()
	{
		$r = '';
		if($tables = $this->cms->session->getNavigation()){
			$r = '<ul id="bb_main_nav_tables">';
			
			foreach($tables as $row){
			
				$r .= '
				<li>
				<span>' . $row['name'] . '</span>
				<ul>';
				
				foreach($row['tables'] as $item){
					
					$q_label = $this->db->queryRow("SELECT display_name,menu_id FROM ".BLACKBIRD_TABLE_PREFIX."tables WHERE table_name = '$item'");
					if($q_label['display_name'] == ''){
						$label = Utils::formatHumanReadable($item);
					}else{
						$label = $q_label['display_name'];
					}
					
					$r .= '<li><a href="' . BLACKBIRD_ROOT . 'browse/' . $item . '">' . $label . '</a></li>'; 
				}
			
			
			$r .= '</ul>
			</li>';
			
			}
			
			$r .= '</ul>';
		}
		return $r;
	}
	
	public function actions()
	{
		return '<ul id="bb_main_nav_actions">
			<li id="bb_nav_action_expand">Expand</li>
			<li id="bb_nav_action_collapse">Collapse</li>
		</ul>';
	}

	function css()
	{
		// Default CSS files
		$this->css[] = 'reset.css';
		$this->css[] = 'prototype.css';
		$this->css[] = 'prototype_edit.css';
		$this->css[] = 'data.css';
		//$this->css[] = 'edit.css';
		$this->css[] = 'imagebrowser.css';
		// Remove duplicates
		$files = array_unique($this->css);
		// Build links
		$r = '';
		foreach ($files as $filename) {
			$r .= sprintf(
				'<link rel="stylesheet" href="%s" type="text/css" media="screen" charset="utf-8" />',
				BLACKBIRD_ROOT . ASSETS . 'css/' . $filename
			);
		}
		return $r;
	}
	
	function ieConditionals()
	{
		$r = '';
		$r .= sprintf(
			'<link rel="stylesheet" href="%s" type="text/css" media="screen" charset="utf-8" />',
			BLACKBIRD_ROOT . ASSETS . 'css/' . 'prototype_ie7.css'
		);
		return '<!--[if IE 7]>'.$r.'<![endif]-->';
	}
	
	function feed()
	{
		$r = '<link rel="alternate" type="application/atom+xml" title="Blackbird Activity" href="'. BLACKBIRD_ROOT .'feed/activity/md5stringlopiasdfuip" />';
		return $r;
	}
	
}